package com.tarena.lbs.business.controller;

import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.pojo.dto.AttachQrDTO;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.param.UserLocationParam;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.FileVO;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.servcie.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "店铺相关功能接口")
@RestController
@Slf4j
public class StoreController {
    @Autowired
    private StoreService storeService;


    @ApiOperation(value = "查询店铺列表")
    @PostMapping("/admin/basic/store/getAll")
    public Result<PageResult<StoreVO>> pageList(@RequestBody StoreQuery query)
        throws BusinessException {
        log.info("查询店铺列表{}",query);
        PageResult<StoreVO> pageResult = storeService.storeList(query);
        return new  Result<>(pageResult);
    }

    /**
     * 新增店铺
     * @param param
     * @return
     * @throws BusinessException
     */

    @ApiOperation(value = "新增店铺")
    @PostMapping("/admin/basic/store/add")
    public Result<Void> save(@RequestBody StoreParam param)
        throws BusinessException {
        log.info("新增店铺{}",param);
        storeService.add(param);
        return Result.success();
    }
   //保存商店图片和logo
   @DubboReference
   AttachApi  attachApi;
    @ApiOperation(value = "保存商店图片和logo" )
    @PostMapping("/admin/basic/store/saveStorePhoto")
    public Result<AttachQrDTO> saveStoreImages(@RequestPart("file") MultipartFile file)
            throws Exception {
        if (file.isEmpty()) {
            return Result.fail("1","请上传文件");
        }
        // 先读取为 byte[] 或保存到临时路径
        byte[] fileBytes = file.getBytes();

        // 调用 Dubbo 服务时只传 byte[] 或文件路径

        // 打印文件信息
        System.out.println("文件名: " + file.getOriginalFilename());
        System.out.println("文件大小: " + file.getSize());
        System.out.println("文件类型: " + file.getContentType());
//        FileVO fileVO= attachApi.saveAttachNo(file);
        AttachQrDTO fileInfo = attachApi.saveAttachNo(fileBytes, file.getOriginalFilename());
        // TODO: 保存文件到服务器


        return new Result<>(fileInfo);
    }


//    查询 属于当前登录用户 所属商家 和满足区域条件的店铺列表
    @ApiOperation(value = "查询区域下的店铺列表")
    @PostMapping("/admin/basic/store/getStoreByCity")
    public Result<PageResult<StoreVO>> getStoreByCity(@RequestBody AreaStoreQuery query)
        throws BusinessException {
        log.info("查询区域下的店铺列表{}",query);
        PageResult<StoreVO> pageResult = storeService.getStoreByCity(query);
        return new  Result<>(pageResult);
    }
    @ApiOperation(value = "删除店铺")
    @PostMapping("/admin/basic/store/delete/{id}")
    public Result getStoreByCity(@PathVariable Integer id)
        throws BusinessException {
        log.info("删除的店铺{}",id);
         int count = storeService.delete(id);
         if(count > 0){
             return Result.success();
         }else{
        return Result.fail();}
    }
    //状态修改
    @ApiOperation(value = "状态修改")
    @PostMapping("/admin/basic/store/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Integer id,@PathVariable Integer status)
        throws BusinessException{
        log.info("状态修改{}{}",id,status);
        int count = storeService.updateStatus(id,status);
        if(count>0){
        return Result.success();
        } else{return  Result.fail();}
    }
    //手机小程序定位上报
    @ApiOperation(value = "手机小程序定位上报")
    @PostMapping("/basic/store/location")
    public Result<Void> location(@RequestBody UserLocationParam param)
        throws BusinessException{
        log.info("手机小程序定位上报{}",param);
        StoreVO locationPO = new StoreVO();
        locationPO.setUserId(param.getUserId());
        locationPO.setStoreLongitude(param.getLongitude());
        locationPO.setStoreLatitude(param.getLatitude());
        storeService.location(locationPO);
        return Result.success();
    }

}
